import { createSlice } from "@reduxjs/toolkit";
import { RootState } from "../../store";

interface SetState {
  isShowModule: boolean;
  selectedContentId: number | undefined;
}

const initialState = {
  isShowModule: false,
  selectedContentId: undefined,
} as SetState;

export const SetSlice = createSlice({
  name: "set",
  initialState,
  reducers: {
    onShowSetModule(state) {
      state.isShowModule = true;
    },
    onCloseSetModule(state) {
      state.selectedContentId = undefined
      state.isShowModule = false;
    },
    onSetSelectContentId(state, action) {
      state.selectedContentId = action.payload;
    },
  },
});

export const SetActions = SetSlice.actions;

export const selectIsShowSetModule = (state: RootState) => {
  return state.set.isShowModule;
};

export const selectedContent = (state: RootState) => {  
  const ret = state.home.contents.filter(
    (content) => content.id === state.set.selectedContentId
  );
  return ret.length > 0 ? ret[0] : undefined;
}